package io.mft.as.ar.gateway.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import io.mft.as.ar.gateway.client.GateWayManager;
import io.mft.as.ar.gateway.config.bean.ARConfig;
import io.mft.as.ar.gateway.exception.GatewayClientException;

/**
 * 描述: 版权: Copyright (c) 2012 公司: 思迪科技 作者: 李炜 版本: 1.0 创建日期: Nov 25, 2013 创建时间:
 * 2:25:52 PM
 */
public class BusConfig
{


	private static Map<String, ConcurrentHashMap<String, String>> SERVER_CONFIG = new ConcurrentHashMap<String, ConcurrentHashMap<String, String>>();
	static ConcurrentHashMap<String, String> serverKey=new ConcurrentHashMap<String, String>();
	// 配置文件最后更新时间
	private static Long CONFIG_LAST_MODIFIED_TIME = 0L;
	private final static String CONFIG_FILE_NAME = "BusConfig.xml";
	// 默认的BUS服务器信息
	// 默认的BUS服务器信息
	private static String DEFAULT_SERVER_ID = "";
	private static BusConfig instance=new BusConfig();
	public static BusConfig getInstance() {
		return instance;
	}
	static
	{
		loadConfigV2();
	}

	private BusConfig()
	{
	}

	public static boolean addConfig(ARConfig config) {
		String key=config.getAr_id()+config.getAr_gateway()+config.getAr_lbmode()+config.getAr_poolsize()+config.getAr_recvtimeout()+config.getAr_scaninterval()+config.getAr_mode();
		boolean s=false;
		if (!serverKey.containsKey(key)&&SERVER_CONFIG.containsKey(config.getAr_id())) {
			buildConfig(config);
			GateWayManager.modifiedServerInfo();
			serverKey.put(key,key);
			s=true;
		}
		if (!SERVER_CONFIG.containsKey(config.getAr_id())) {
			buildConfig(config);
			GateWayManager.init();
			serverKey.put(key,key);
			s=true;
		}
		return s;
	}


	private static void buildConfig(ARConfig config) {
		String id=config.getAr_id();
		String version=config.getAr_version();
		String gateway=config.getAr_gateway();
		String scanInterval=config.getAr_scaninterval();
		String LBMode=config.getAr_lbmode();
		String Mode=config.getAr_mode();
		String poolSize=config.getAr_poolsize();
		String recvTimeout=config.getAr_recvtimeout();
		ConcurrentHashMap<String, String> arServer = new ConcurrentHashMap<String, String>();
		arServer.put("version", version);
		arServer.put("gateway", gateway);
		arServer.put("scanInterval", scanInterval);
		arServer.put("LBMode", LBMode);
		arServer.put("Mode", Mode);
		arServer.put("poolSize", poolSize);
		arServer.put("recvTimeout", recvTimeout);
		SERVER_CONFIG.put(id, arServer);
	}
	private synchronized static void loadConfigV2()
	{
		/**
		try
		{
			Document document = XMLHelper.getDocument(Configuration.class, CONFIG_FILE_NAME);
			if(document == null)
			{
				logger.error("未找到配置文件[" + CONFIG_FILE_NAME + "]");
				return;
			}
			Element rootElement = document.getRootElement();

			String defaultServerId = rootElement.attributeValue("default", "");// 默认执行的服务器信息
			String firstServerId = "";// BusConfig.xml中配置的第一个节点信息

			Map<String, ConcurrentHashMap<String, String>> serverConfieMap = null;

			int i = 0;
			List<Element> serverList = rootElement.elements("server");
			if(serverList != null && !serverList.isEmpty())
			{
				serverConfieMap = new HashMap<String, ConcurrentHashMap<String, String>>();
				for(Element serverElement : serverList)
				{
					String serverId = serverElement.attributeValue("id");
					if(StringHelper.isEmpty(serverId))
					{
						continue;
					}

					if(i == 0)
					{
						firstServerId = serverId;
						i++;
					}

					ConcurrentHashMap<String, String> propMap = new ConcurrentHashMap<String, String>();
					List<Element> itemList = serverElement.elements("property");
					for(Element itemElement : itemList)
					{
						String itemName = itemElement.attributeValue("name");
						String value = itemElement.attributeValue("value");
						if(!ObjectUtil.isEmpty(itemName))
						{
							propMap.put(itemName, value);
						}
					}

					serverConfieMap.put(serverId, propMap);
				}
			}

			if(serverConfieMap != null)
			{
				SERVER_CONFIG.putAll(serverConfieMap);
				serverConfieMap.clear();
			}

			
			// 如果配置了default，需要比较default的值是否与server的id一致，如果不一致，取第一个server的id作为默认值
			 
			if(ObjectUtil.isEmpty(defaultServerId) || !SERVER_CONFIG.containsKey(defaultServerId))
			{
				defaultServerId = firstServerId;
			}
			DEFAULT_SERVER_ID = defaultServerId;

		}
		catch(Exception ex)
		{
			logger.error("", ex);
		}
**/
	}
	

	/**
	 * 获得字串配置值
	 *
	 * @param name
	 * @return
	 */
	public static String getString(String serverName, String key)
	{
		
		return MapUtil.getStr(getServerMap(serverName), key);
	}

	
	
	/**
	 * 获得字串配置值，若为空，则返回缺省值
	 *
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static String getString(String serverName, String key, String defaultValue)
	{
		String value = getString(serverName, key);
		return ObjectUtil.isEmpty(value) ? defaultValue : value;
	}

	/**
	 * 获得整型配置值
	 *
	 * @param name
	 * @return
	 */
	public static int getInt(String serverName, String key)
	{
		return MapUtil.getInt(getServerMap(serverName), key);
	}

	/**
	 * 获得整型配置值
	 *
	 * @param name
	 * @return
	 */
	public static int getInt(String serverName, String key, int defaultValue)
	{
		int value = getInt(serverName, key);
		return (value == 0) ? defaultValue : value;
	}

	/**
	 * 获得布尔型配置值
	 *
	 * @param name
	 * @return
	 */
	public static boolean getBoolean(String serverName, String key)
	{
		return MapUtil.getBool(getServerMap(serverName), key);
	}

	/**
	 * 获得双精度浮点数配置值
	 *
	 * @param name
	 * @return
	 */
	public static double getDouble(String serverName, String key)
	{
		return MapUtil.getDouble(getServerMap(serverName), key);
	}

	/**
	 *
	 * 描述： 作者：李炜 时间：Nov 26, 2013 9:40:35 AM
	 *
	 * @return
	 */
	public static Map<String, ConcurrentHashMap<String, String>> getServerConfig()
	{
		return SERVER_CONFIG;
	}

	/**
	 *
	 * 描述：获取默认执行的服务器信息ID 如果BusConfig.xml只配置了一个BUS服务器信息，这里返回那个BUS服务器的ID
	 * 如果BusConfig.xml配置了多个BUS服务器信息，先比较servers中配置的default值与server的id是否一致，如果不一致，
	 * 取最后一个server的id作为默认ID 作者：李炜 时间：Apr 27, 2014 1:16:39 PM
	 *
	 * @return
	 */
	public static String getDefaultServerId()
	{
		return DEFAULT_SERVER_ID;
	}

	/**
	 *
	 * 描述： 作者：李炜 时间：Nov 26, 2013 11:35:03 AM
	 *
	 * @param id
	 * @return
	 */
	public static ConcurrentHashMap<String, String> getServerMap(String id)
	{
		if(SERVER_CONFIG.containsKey(id))
		{
			return SERVER_CONFIG.get(id);
		}
		else
		{
			throw new GatewayClientException(-1, "服务器信息[" + id + "]配置不存在");
		}

	}
}
